package com.enhe.gck.fg;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.enhe.gck.communal.PublicEntity;
import com.enhe.gck.communal.PublicMethod;
import com.enhe.gck.communal.PublicReturn;
import com.enhe.gck.item.OrderHWANE16677Item;
import com.enhe.gck.item.RechargeHWANE16698Item;
import com.enhe.gck.pojo.MoneyXiaoXiaoLe;
import com.enhe.gck.pojo.Order;
import com.enhe.gck.pojo.Recharge;
import com.enhe.gck.pojo.WeighOther;
import com.kuangkie.carbon.enun.SortDirection;
import com.kuangkie.carbon.fg.*;
import com.kuangkie.carbon.panel.CarbonPanel;
import com.kuangkie.carbon.panel.RecordDiscover;
import com.kuangkie.carbon.record.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import com.enhe.gck.CarbonBaseConstant; //模型Code
import com.enhe.gck.CarbonEnumKeyValue; //枚举字典信息
import com.enhe.gck.CarbonRelationType; //关系类型Code
import com.enhe.gck.item.MoneyXiaoXiaoLeHWANE25759Item; // 模型属性Code

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Repository(value = "hwane25759366042330866425862")
public class MoneyXiaoXiaoLeHWANE25759BNB implements SecondRoundImproveFuncGroup, FetchFuncGroup {
	
	@Override
	public void secondImprove(FuncGroupContext context, ProRecord proRecord, FGRecordComplexus recordComplexus,
			ImproveResultFactory improveResultFactory) {
		MoneyXiaoXiaoLe info=recordComplexus.getRecordBean(proRecord.getRecordCode(),MoneyXiaoXiaoLe.class,2);
		if(!proRecord.isNew()&&String.valueOf(CarbonEnumKeyValue.操作命令_sb天天爱消除_ttaxc).equals(info.getCommand())){
			if(info.getMoneyXc()!=null&&info.getMoneyXc().compareTo(BigDecimal.ZERO)>0){
				List<Recharge> list=new ArrayList<>();
				BigDecimal sum=getMoney(info.getCustomer().getRecordCode(),info.getGoods().getRecordCode(),list);
				if(sum.compareTo(info.getMoneyXc())>=0){
					BigDecimal sumNew=info.getMoneyXc();
					for(Recharge r:list){
						BigDecimal ye=new BigDecimal(String.valueOf(r.getAvailableBalance()));
						if(sumNew.compareTo(ye)>0){
							sumNew=sumNew.subtract(ye);
							PublicMethod.updateAny(improveResultFactory,CarbonBaseConstant.充值记录_czjl,r.getRecordCode())
									.putAttribute(RechargeHWANE16698Item.基本属性组_可用余额,BigDecimal.ZERO)
									.putAttribute(RechargeHWANE16698Item.基本属性组_实际消除,r.getMoneyXc()==null?ye:r.getMoneyXc().add(ye));
						}else {
							PublicMethod.updateAny(improveResultFactory,CarbonBaseConstant.充值记录_czjl,r.getRecordCode())
									.putAttribute(RechargeHWANE16698Item.基本属性组_可用余额,ye.subtract(sumNew))
									.putAttribute(RechargeHWANE16698Item.基本属性组_实际消除,r.getMoneyXc()==null?sumNew:r.getMoneyXc().add(sumNew));
							OperationLogHWANE21797BNB.save(improveResultFactory,9,10,info.getCustomer().getRecordCode(),info.getGoods().getName()+": "+info.getMoneyXc().toPlainString()+" $");
							break;
						}
					}
				}else {
					PublicMethod.returnMessage(improveResultFactory, PublicReturn.Recharge_8);
					return;
				}
			}else {
				PublicMethod.returnMessage(improveResultFactory, PublicReturn.Recharge_8);
				return;
			}
		}
	}
	@Override
	public void fetchImprove(FuncGroupContext funcGroupContext, FGRecord fgRecord, FetchResultFactory fetchResultFactory) {
		FGRelationCorrelation relation=CarbonPanel.getRecordDiscover(CarbonBaseConstant.余额消除_yexc).getRelationCorrelation(fgRecord.getRecordCode());
		String khId=relation.getGuestRecordCode(CarbonRelationType.RR_余额消除_客户_客户管理);
		String cpId=relation.getGuestRecordCode(CarbonRelationType.RR_余额消除_产品_产品管理);
		boolean yes=StrUtil.isNotBlank(khId)&&StrUtil.isNotBlank(cpId);
		fetchResultFactory.getRecordOpsBuilder().putAttribute(MoneyXiaoXiaoLeHWANE25759Item.基本属性组_余额,yes?getMoney(khId,cpId,null):BigDecimal.ZERO);
	}
	public static BigDecimal getMoney(String khId,String cpId,List<Recharge> listc) {
		BigDecimal ye=BigDecimal.ZERO;
		RecordQueryProConJunctionFactory jf = CarbonPanel
				.getRecordQueryFactory(CarbonBaseConstant.充值记录_czjl).getProConJunctionFactory()
				.addGreaterThan(RechargeHWANE16698Item.基本属性组_可用余额,0)
				.addEqual(RechargeHWANE16698Item.基本属性组_作废,false);
		jf.getRelationCriterionFactory(CarbonBaseConstant.客户管理_khgl)
				.setInRelationTypes(CarbonRelationType.RR_充值记录_客户名称_客户管理)
				.setInGuestCodes(khId);
		List<Recharge> list=jf.goBack().addSortItem(PublicMethod.createTimeStr(CarbonBaseConstant.充值记录_czjl), SortDirection.ASC)
				.queryBeans(Recharge.class,3);
		if(list!=null&&!list.isEmpty()){
			for(Recharge r:list){
				if(cpId.equals(r.getUnivalence().getGoods().getRecordCode())){
					if(listc!=null){
						listc.add(r);
					}
					ye=ye.add(new BigDecimal(String.valueOf(r.getAvailableBalance())));
				}
			}
		}
		return ye;
	}
}
